package com.heima.article.service.impl;
import java.util.Date;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.ApCollectionService;
import com.heima.common.exception.CustException;
import com.heima.feigns.behavior.BehaviorFeign;
import com.heima.model.article.dto.CollectionBehaviorDto;
import com.heima.model.article.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppUserThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper,ApCollection> implements ApCollectionService {
    @Autowired
    BehaviorFeign behaviorFeign;
    /**
     * 保存收藏方法
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collection(CollectionBehaviorDto dto) {
        //判断用户登录状态
        ApUser user = AppUserThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry;
        if (user != null) {
            //根据用户id查询用户行为实体
            apBehaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(user.getId(), 0);
        } else {
            apBehaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(null, dto.getEquipmentId());
        }
        if(apBehaviorEntry==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"用户行为实体数据不存在");
        }
        //判断操作类型
        if(dto.getOperation()==(short)0){
            //添加收藏
            addCollection(dto,apBehaviorEntry);
        }else {
            //删除收藏
            deleteCollection(dto,apBehaviorEntry);
        }
    return ResponseResult.okResult();
    }

    /**
     * 删除收藏方法
     * @param dto
     * @param apBehaviorEntry
     */
    private void deleteCollection(CollectionBehaviorDto dto, ApBehaviorEntry apBehaviorEntry) {
        remove(Wrappers.<ApCollection>lambdaQuery()
                .eq(ApCollection::getArticleId,dto.getEntryId())
                .eq(ApCollection::getEntryId,apBehaviorEntry.getId()));
    }

    /**
     * 添加收藏方法
     * @param dto
     * @param apBehaviorEntry
     */
    private void addCollection(CollectionBehaviorDto dto, ApBehaviorEntry apBehaviorEntry) {
        //根据行为实体对象id和文章id判断是否收藏
        ApCollection apCollection = getOne(Wrappers.<ApCollection>lambdaQuery()
                .eq(ApCollection::getArticleId, dto.getEntryId())
                .eq(ApCollection::getEntryId, apBehaviorEntry.getId()));
        if(apCollection!=null){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"文章已经收藏了");
        }
        apCollection = new ApCollection();
        apCollection.setEntryId(apBehaviorEntry.getId());
        apCollection.setArticleId(dto.getEntryId());
        apCollection.setType((short)0);
        apCollection.setCollectionTime(new Date());
        save(apCollection);
    }
}
